<?php

set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");

require_once($root."initialize.php");

if(($db_msg=cron_initialize()) !== true){

	print_r("Connection error");

}else{ 

	$db=$vars["db"];
	
	$dist = array();
	$dist2 = array();
	
	$day1 = date("Y-m-d",mktime(0, 0, 0 , date('m'), date('d')-10, date("Y")));
	$day7 = date("Y-m-d",mktime(0, 0, 0 , date('m'), date('d')-4, date("Y")));
	$cdate = date('Y-m-d H:i:s');
		
	$r=mysql_query("select d.id,d.ref,d.rebate_percentage from gg_users d inner join gg_users s on s.id=809 and d.ref_left between s.ref_left and s.ref_right");
	while($user = mysql_fetch_assoc($r))
	{
		$dist[$user['id']]['ref']=$user['ref'];
		$dist[$user['id']]['rebate_percentage']=$user['rebate_percentage'];
		$dist[$user['id']]['amount']=0;
	}	

	$rs_sales=mysql_query("select d.id,d.rebate_percentage, sum(e.amount) as amount from gg_users d inner join gg_users s on s.id=809 and d.ref_left between s.ref_left and s.ref_right inner join gg_member_eswallet_record e on e.uid=d.id and e.aid>0 and date_format(e.cdate, '%Y-%m-%d') between '{$day1}' and '{$day7}' group by d.id");
	while($sales = mysql_fetch_assoc($rs_sales)){
		$ref = $sales['id'];
		$amount = $sales['amount'] * 0.92;
		$current_percentage = number_format($sales['rebate_percentage'], 0);
		if($current_percentage>0){
			$rebate_amount = number_format(($amount * ($current_percentage/100)), 2, ".", "");
			$dist[$ref]['amount'] += $rebate_amount;
			$dist[$ref]['percent'][$current_percentage] += $rebate_amount;
		}
		$ref = $dist[$ref]['ref'];

		while($ref>0){
			if($dist[$ref]['rebate_percentage']>$current_percentage){
				$percentage = $dist[$ref]['rebate_percentage'] - $current_percentage;
				$current_percentage = $dist[$ref]['rebate_percentage'];
				
				$rebate_amount = number_format(($amount * ($percentage/100)), 2, ".", "");
				$dist[$ref]['amount'] += $rebate_amount;
				$dist[$ref]['percent'][$percentage] += $rebate_amount;
			}
				
			$ref = $dist[$ref]['ref'];			
		}
	}
	
	foreach ($dist as $id=>$avalue){		
		if($avalue['amount']>0){
			$remark = "Rebate for ".$day1." till ".$day7;
			foreach ($avalue['percent'] as $percent=>$avalue2){
				$remark.="\n".$percent."% = ".$avalue2;
			}
			
			$balance=mysql_result(mysql_query("select eswallet from gg_users where id=".$id), 0) or 0;			
			$balance+=$avalue['amount'];
			mysql_query("update gg_users set eswallet=".$balance." where id=".$id);			
			$sql = "INSERT INTO gg_member_eswallet_record SET uid=".$id.",aid='-1',type='credit',amount=".$avalue['amount'].",bal=".$balance.",descr='".$remark."',cdate='".$cdate."'";
			mysql_query($sql);
		}		
	}
	
	
	$r=mysql_query("select d.id,d.ref,d.rebate_percentage from gg_users d inner join gg_users s on s.id=357 and d.ref_left between s.ref_left and s.ref_right");
	while($user = mysql_fetch_assoc($r))
	{
		$dist2[$user['id']]['ref']=$user['ref'];
		$dist2[$user['id']]['rebate_percentage']=$user['rebate_percentage'];
		$dist2[$user['id']]['amount']=0;
	}	
	
	$rs_sales=mysql_query("select d.id,d.rebate_percentage, sum(e.amount) as amount from gg_users d inner join gg_users s on s.id=357 and d.ref_left between s.ref_left and s.ref_right inner join gg_member_eswallet_record e on e.uid=d.id and e.aid>0 and date_format(e.cdate, '%Y-%m-%d') between '{$day1}' and '{$day7}' group by d.id");
	while($sales = mysql_fetch_assoc($rs_sales)){
		$ref = $sales['id'];
		$current_percentage = number_format($sales['rebate_percentage'], 0);
		if($current_percentage>0){
			$rebate_amount = number_format(($sales['amount'] * ($current_percentage/100)), 2, ".", "");
			$dist2[$ref]['amount'] += $rebate_amount;
			$dist2[$ref]['percent'][$current_percentage] += $rebate_amount;
		}
		$ref = $dist2[$ref]['ref'];

		while($ref>0){
			if($dist2[$ref]['rebate_percentage']>$current_percentage){
				$percentage = $dist2[$ref]['rebate_percentage'] - $current_percentage;
				$current_percentage = $dist2[$ref]['rebate_percentage'];
				
				$rebate_amount = number_format(($sales['amount'] * ($percentage/100)), 2, ".", "");
				$dist2[$ref]['amount'] += $rebate_amount;
				$dist2[$ref]['percent'][$percentage] += $rebate_amount;
			}
				
			$ref = $dist2[$ref]['ref'];			
		}
	}
	
	foreach ($dist2 as $id=>$avalue){		
		if($avalue['amount']>0){
			$remark = "Rebate for ".$day1." till ".$day7;
			foreach ($avalue['percent'] as $percent=>$avalue2){
				$remark.="\n".$percent."% = ".$avalue2;
			}
			
			$balance=mysql_result(mysql_query("select eswallet from gg_users where id=".$id), 0) or 0;			
			$balance+=$avalue['amount'];
			mysql_query("update gg_users set eswallet=".$balance." where id=".$id);			
			$sql = "INSERT INTO gg_member_eswallet_record SET uid=".$id.",aid='-1',type='credit',amount=".$avalue['amount'].",bal=".$balance.",descr='".$remark."',cdate='".$cdate."'";
			mysql_query($sql);
		}		
	}
	
	print_r('finish');
}

exit;
?>